package utils;

import java.awt.Desktop;
import java.io.File;
import java.net.URI;
import java.io.IOException;
import java.io.FilenameFilter;
import java.io.RandomAccessFile;
import java.net.URISyntaxException;
import java.security.GeneralSecurityException;
import communication.ClientSecureConnectionControlled;
import communication.Connection;
import communication.ConnectionException;
import security.MessageDigestSHA1;

public final class MachineUtils {


    public static void createEmptyFile(final String directory, final String filename,
            final long length)
            throws IOException {

        RandomAccessFile accessFile = null;
		File file = null;

		try {

			file = new File(filename);

			if(file.exists()){
				file.delete();
				file.createNewFile();
			}

			accessFile = new RandomAccessFile(file,"rw");
			accessFile.setLength(length);
			accessFile.close();

			file = null;
			accessFile = null;

		} catch (IOException e) {
			try{
				if(accessFile!=null)
					accessFile.close();
			} catch(final IOException ex){}
			throw new IOException(e);
		}	
    }

    public static boolean existsFile(final String directory, final String filename) {
        File file = new File(directory,filename);

        return file.exists();
    }

    
    public static long getFileLength(final String directory, final String filename) {

        File file = new File(directory,filename);

		long length = file.length();

        return length;
    }

    
    public static byte[] getHashFile(final String directory, final String filename)
            throws GeneralSecurityException, IOException {

        File file = new File(directory,filename);
		MessageDigestSHA1 md = new MessageDigestSHA1();

		byte [] hashFile = md.digest(file);

		return hashFile;
    }

    
    public static String [] getFilesNames(final String directory, final String extension) {

        File file = new File(directory);

		return file.list(new FilenameFilter(){
			public boolean accept(File file, String name) {
				return name.endsWith(extension);
			}
		});
    }

    
    public final static long getTotalSpace(final String dir) {
        File directory  = new File(dir);
        long totalSpace = directory.getTotalSpace();

        directory = null;

        return totalSpace;
    }


    public final static long getFreeSpace(final String dir) {
        File directory = new File(dir);
        long freeSpace = directory.getFreeSpace();

        directory = null;

        return freeSpace;
    }

    
    public final static boolean deleteFile(final String dir, final String filename) {
        File    file   = new File(dir, filename);
        boolean delete = file.delete();

        file = null;

        return delete;
    }

    
	public final static Connection getSecureConnectionControlled(String hostname,int port,int bandwidth)
            throws ConnectionException{
		return new ClientSecureConnectionControlled(hostname,port,bandwidth);
	}


    public static long getUsedSpace(String [] listFiles) {

        long usedSpace = 0;

        if(listFiles!=null)
            for(int i = 0 ; i < listFiles.length;i++)
                usedSpace += new File(listFiles[i]).length();
        return usedSpace;
    }


    public final static boolean openBrowser(String url)
            throws URISyntaxException, IOException
    {

        if(Desktop.isDesktopSupported())
        {

            URI uri = new URI(url);
            Desktop.getDesktop().browse(uri);

            return true;
        }
        else
        {
            return false;
        }
    }
    
    
    public final static boolean openEmail(String address, String subject, String message)
            throws URISyntaxException, IOException
    {

        if(Desktop.isDesktopSupported())
        {
            String uriString = "?SUBJECT="+subject+"&BODY="+message;

            URI uri = new URI("mailto",uriString,null);
            
            Desktop.getDesktop().mail(uri);

            return true;
        }
        else
        {
            return false;
        }
    }


    
}
